package database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import capsules.CapsuleTemporarySpecification;

public class DatabaseOperationsCapsulesTemporary {
	
	
	private ConnectionDB conn;
		
	
	public DatabaseOperationsCapsulesTemporary(ConnectionDB conn){
		this.conn = conn;
	}
	

	public  void insertCapsuleTemporay(String nameCapsule, String email, int id)
            throws SQLException{
		conn.executeUpdate("call insertCapsuleTemporary('"+nameCapsule+"','"+email+"',"+id+")");
	}


	public boolean existsCapsuleTemporay(String nameCapsule, String email, int id)
            throws SQLException{

		boolean exists = false;	

		ResultSet result  = conn.executeSelect("call existsCapsuleTemporary('"+nameCapsule+"','"+email+"',"+id+")");

		if(result.next())			 
			exists = true;

		result.close();
		conn.closeStatement();

		return exists;
	}
	
	
	public  boolean isCompleteCapsuleTemporay(String nameCapsule, String email, int id)
	throws SQLException{

        ResultSet result  = conn.executeSelect("call completyCapsuleTemporary('"+nameCapsule+"','"+email+"',"+id+")");
	
        boolean isComplety = false;
     
		if(result.next())			 
			isComplety = result.getBoolean("isComplety");

		result.close();
		conn.closeStatement();

		return isComplety;
	}
	
	
	public void setCapsuleTemporaryComplety(String nameCapsule, String email, int  id)
	throws SQLException{	
		conn.executeUpdate("call updateCapsuleTemporaryComplety('"+nameCapsule+"','"+email+"',"+id+")");
	}
		

	public  void deleteCapsuleTemporary(String nameCapsule, String email, int id)
	throws SQLException {
		conn.executeUpdate("call deleteCapsuleTemporary('"+nameCapsule+"','"+email+"',"+id+")");
	}
	
	
	public ArrayList<CapsuleTemporarySpecification> getListCapsuleTemporary()
	throws SQLException{
	
		ResultSet result = conn.executeSelect("call listCapsuleTemporaryComplete()");
		
		ArrayList<CapsuleTemporarySpecification> list = new ArrayList<CapsuleTemporarySpecification>();
		
		while(result.next())
			list.add(new CapsuleTemporarySpecification(result.getString("nameCapsule"),result.getString("email"),result.getInt("id")));
		
		result.close();
		result = null;
		
		return list;
	}
		
	
	public void close()
            throws SQLException{
		if(conn!=null){
			conn.close();
			conn = null;
		}
	}
	
	
}
